package com.example.myshop.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myshop.domain.ProductDao;
import com.example.myshop.entity.Product;
import com.example.myshop.service.ProductService;
import com.example.myshop.service.ex.InsertException;
import com.example.myshop.service.ex.ProductExistedException;
import com.example.myshop.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImp extends ServiceImpl<ProductDao, Product> implements ProductService {
    @Autowired
    private ProductDao productDao;

    @Override
    public List<Product> findHotList() {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("priority");
        queryWrapper.last("limit 0,4");
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public List<Product> findMarkList() {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("judge_mark");
        queryWrapper.last("limit 0,4");
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public List<Product> findDescList() {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("price");
        queryWrapper.last("limit 0,4");
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public List<Product> findAscList() {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",1);
        queryWrapper.orderByAsc("price");
        queryWrapper.last("limit 0,4");
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public Product findById(Long id) {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Product product=productDao.selectOne(queryWrapper);
        if (product == null) {
            // 是：抛出ProductNotFoundException
            throw new ProductNotFoundException("尝试访问的商品数据不存在");
        }
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);
        return product;
    }

    @Override
    public List<Product> findByTitle(String title) {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.like("title",title);
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            if (product == null) {
                // 是：抛出ProductNotFoundException
                throw new ProductNotFoundException("尝试访问的商品数据不存在");
            }
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public List<Product> findAllProducts() {
        List<Product>products=productDao.selectList(null);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public List<Product> findAllPassProducts() {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public List<Product> findAllProductsByBid(Long bid) {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("business_id",bid);
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;

    }

    @Override
    public List<Product> findPassProductsByBid(Long bid) {
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("business_id",bid);
        queryWrapper.eq("status",1);
        List<Product>products=productDao.selectList(queryWrapper);
        for (Product product : products) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return products;
    }

    @Override
    public void insert(Product product) {
        Long pid=product.getId();
        Product result=productDao.selectById(pid);
        if (result != null) {
            throw new ProductExistedException("商品已经存在");
        }
        Date date =new Date();
        product.setCreatedUser("商家");
        product.setModifiedUser("管理员");
        product.setCreatedTime(date);
        product.setModifiedTime(date);

        Integer rows= productDao.insert(product);
        if(rows!=1){
            throw new InsertException("插入商品产生未知异常");
        }
    }

    @Override
    public void passProjectById(Long id) {
        UpdateWrapper<Product>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",1);
        updateWrapper.set("modified_user","root");
        updateWrapper.set("modified_time",new Date());
        productDao.update(null,updateWrapper);
    }

    @Override
    public void rejectProjectById(Long id) {
        UpdateWrapper<Product>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",0);
        updateWrapper.set("modified_user","root");
        updateWrapper.set("modified_time",new Date());
        productDao.update(null,updateWrapper);
    }

    @Override
    public void passProjectByBusiness(Long id) {
        UpdateWrapper<Product>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",1);
        updateWrapper.set("modified_user","商家");
        updateWrapper.set("modified_time",new Date());
        productDao.update(null,updateWrapper);
    }

    @Override
    public void rejectProjectByBusiness(Long id) {
        UpdateWrapper<Product>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",2);
        updateWrapper.set("modified_user","商家");
        updateWrapper.set("modified_time",new Date());
        productDao.update(null,updateWrapper);
    }
}
